// global variables //
var TIMER = 5;
var SPEED = 35;
var WRAPPER = 'content';

// calculate the current window width //
function pageWidth() {
  return window.innerWidth != null ? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}

// calculate the current window height //
function pageHeight() {
  return window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}

// calculate the current window vertical offset //
function topPosition() {
  return typeof window.pageYOffset != 'undefined' ? window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}

// calculate the position starting at the left of the window //
function leftPosition() {
  return typeof window.pageXOffset != 'undefined' ? window.pageXOffset : document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}

// build/show the dialog box, populate the data and call the fadeDialog function //
function showDialog(title,message,type,autohide) {
  if(!type) {
    type = 'error';
  }
  var dialog;
  var dialogheader;
  var dialogclose;
  var dialogtitle;
  var dialogcontent;
  var dialogmask;
  var dialogiframe;
  if(!document.getElementById('dialog')) {
    dialog = document.createElement('div');
    dialog.id = 'dialog';
    dialogheader = document.createElement('div');
    dialogheader.id = 'dialog-header';
    dialogtitle = document.createElement('div');
    dialogtitle.id = 'dialog-title';
    dialogclose = document.createElement('div');
    dialogclose.id = 'dialog-close'
    dialogcontent = document.createElement('div');
    dialogcontent.id = 'dialog-content';
    dialogmask = document.createElement('div');
    dialogmask.id = 'dialog-mask';

    //透明屏蔽禁止右键
    //dialogmask.attachEvent("oncontextmenu",function(){return false;});
    
    //增加iframe屏蔽select
    dialogiframe = document.createElement("iframe");
    dialogiframe.id= "dialog-iframe";
    dialogiframe.className = "dis";    
    document.body.appendChild(dialogiframe);
    
    document.body.appendChild(dialogmask);
    document.body.appendChild(dialog);
    dialog.appendChild(dialogheader);
    dialogheader.appendChild(dialogtitle);
    dialogheader.appendChild(dialogclose);
    dialog.appendChild(dialogcontent);
    dialogclose.setAttribute('onclick','hideDialog()');
    dialogclose.onclick = hideDialog;

    //增加鼠标拖动事件
    dialogheader.attachEvent("onmousedown",mymousedown);
    dialogheader.attachEvent("onmouseup",mymouseup);
    dialogheader.attachEvent("onmousemove",mymousemove);
  } else {
    dialog = document.getElementById('dialog');
    dialogheader = document.getElementById('dialog-header');
    dialogtitle = document.getElementById('dialog-title');
    dialogclose = document.getElementById('dialog-close');
    dialogcontent = document.getElementById('dialog-content');
    dialogmask = document.getElementById('dialog-mask');
    
    //增加iframe屏蔽select
    dialogiframe = document.getElementById('dialog-iframe');
    dialogiframe.style.visibility = "visible";
    
    dialogmask.style.visibility = "visible";
    dialog.style.visibility = "visible";
  }
  dialog.style.opacity = .00;
  dialog.style.filter = 'alpha(opacity=0)';
  dialog.alpha = 0;

  dialogheader.className = type + "header";
  dialogtitle.innerHTML = title;
  dialogcontent.className = type;
  dialogcontent.innerHTML = message;
  var content = document.getElementById(WRAPPER);
  dialogmask.style.height = content.offsetHeight + 'px';  //  设置屏蔽层的高度

  dialog.timer = setInterval("fadeDialog(1)", TIMER);
  if(autohide) {
    dialogclose.style.visibility = "hidden";
    window.setTimeout("hideDialog()", (autohide * 1000));
  } else {
    dialogclose.style.visibility = "visible";
  }

//  setDialogPosition();
}

function setDialogPosition(){
    //每次先回复header的大小，然后计算dialog的实际大小，然后再设置header大小
    var dialogheader = document.getElementById("dialog-header");
    dialogheader.style.width = "0px";
    var dialog = document.getElementById('dialog');
    dialogheader.style.width = dialog.clientWidth + "px";
    
    var width = pageWidth();
    var height = pageHeight();
    var left = leftPosition();
    var top = topPosition();
    var dialogwidth = dialog.offsetWidth;
    var dialogheight = dialog.offsetHeight;
    var topposition = top + (height / 3) - (dialogheight / 2);
    var leftposition = left + (width / 2) - (dialogwidth / 2);
    dialog.style.top = topposition + "px";
    dialog.style.left = leftposition + "px";
//      alert("width:" + dialogwidth + ",height:" + dialogheight);
}

// hide the dialog box //
function hideDialog() {
  var dialog = document.getElementById('dialog');
  clearInterval(dialog.timer);
  dialog.timer = setInterval("fadeDialog(0)", TIMER);
}

// fade-in the dialog box //
function fadeDialog(flag) {
  if(flag == null) {
    flag = 1;
  }
  var dialog = document.getElementById('dialog');
  var value;
  if(flag == 1) {
    value = dialog.alpha + SPEED;
  } else {
    value = dialog.alpha - SPEED;
  }
  dialog.alpha = value;
  dialog.style.opacity = (value / 100);
  dialog.style.filter = 'alpha(opacity=' + value + ')';
  if(value >= 99) {
    clearInterval(dialog.timer);
    dialog.timer = null;
  } else if(value <= 1) {
    dialog.style.visibility = "hidden";
    document.getElementById('dialog-mask').style.visibility = "hidden";
    document.getElementById('dialog-iframe').style.visibility = "hidden";
    clearInterval(dialog.timer);
  }
}

    var g_x;
    var g_y;
    function mymousedown() {
    	var _header = document.getElementById("dialog-header");
        if (event.button == 1) {
            g_x = event.clientX;
            g_y = event.clientY;
            _header.style.cursor = "move";
            //锁定鼠标事件
            _header.setCapture();
        }
        return true;
    }
    
    function mymousemove() {
    	var theObj = document.getElementById("dialog");
        if (event.button == 1) {
            //计算鼠标指针的移动量
            var deltaX = event.clientX - g_x;
            var deltaY = event.clientY - g_y;
            g_x = event.clientX;
            g_y = event.clientY;
            var pageH = pageHeight();
            var pageW = pageWidth();
            theObj.style.pixelLeft += deltaX;
            theObj.style.pixelTop += deltaY;

            if(theObj.style.pixelTop < 0){
                theObj.style.pixelTop=0;
            }
            if(theObj.style.pixelLeft< 0){
        	    theObj.style.pixelLeft=0;
            }
            if(theObj.style.pixelTop + theObj.offsetHeight> pageH){
        	    theObj.style.pixelTop =pageH-theObj.offsetHeight;
            }
            if(theObj.style.pixelLeft + theObj.offsetWidth> pageW){
        	    theObj.style.pixelLeft=pageW-theObj.offsetWidth;
            }        
        }
        return true;
    }
    
    function mymouseup() {
    	var _header = document.getElementById("dialog-header");
        if (event.button == 1) {
            //放开鼠标事件
            _header.releaseCapture();
            _header.style.cursor="default";
//        alert("mousex():" + event.clientX + ",mousey():" + event.clientY);
//        alert("pageWidth():" + pageWidth() + ",pageHeight():" + pageHeight());
//        alert("divWidth():" + theObj.offsetWidth + ",divHeight():" + theObj.offsetHeight);
        }
        return true;
    }
